package struts.tools;

import org.eclipse.emf.common.util.EList;

import struts.*;
import struts.impl.*;

public class JSPVisitor implements IVisitor{

	@Override
	public void visit(SelectImpl elt, Data data) {
		data.addFormElement(elt);
		
		data.addToJspCode("<select>");		
		EList<Option> options=elt.getOptions();
		for(Option o:options){
			o.accept(this, data);
			
		}		
		data.addToJspCode("</select>\n");
	}

	@Override
	public void visit(PageBr elt, Data data) {
		data.addToJspCode("<br>\n");		
	}
	
	@Override
	public void visit(FormBr elt, Data data) {
		data.addToJspCode("<br>\n");		
	}

	@Override
	public void visit(CheckBox elt, Data data) {
		String jsp="<input type=\"checkbox\" name=\""+elt.getName()+"\" value=\""+elt.getValue()+"\">"+ elt.getValue();
		data.addFormElement(elt);
		data.addToJspCode(jsp+"\n");
	}

	@Override
	public void visit(Button elt, Data data) {
		System.out.println("NOT Implemented: "+"Button");		
	}

	@Override
	public void visit(FormElement elt, Data data) {
		System.out.println("NOT Implemented: "+"FormElement");		
		
	}

	@Override
	public void visit(Form elt, Data data) {
		data.addToJspCode("<html:form action=\"/"+elt.getAction()+"\"> \n<html:errors/>\n");
		data.newFormID();
		EList<FormElement> fields=elt.getFields();
		for(FormElement e:fields){
			e.accept(this, data);
		}
		data.addToJspCode("</html:form>\n");
	}

	
	@Override
	public void visit(Header elt, Data data) {
		data.addToJspCode("<h"+elt.getLevel()+">");		
		elt.getTagText().accept(this, data);		
		data.addToJspCode("</h"+elt.getLevel()+">\n");
		
	}

	
	@Override
	public void visit(Image elt, Data data) {		
		String jsp="<img src=\""+elt.getSrc()+"\" title=\""+elt.getTitle()+"\">\n";
		data.addToJspCode(jsp);
	}

	@Override
	public void visit(Label elt, Data data) {
		String propEntry=WebSiteData.generateLabelID();
		WebSiteData.addRessource(propEntry, elt.getValue());
		String jsp="<bean:message key=\""+propEntry+"\"/>\n";		
		data.addToJspCode(jsp);
	}

	@Override
	public void visit(Link elt, Data data) {
		String propEntry=WebSiteData.generateLabelID();
		WebSiteData.addRessource(propEntry, elt.getValue());
		System.out.println("Je suis dans Link: "+elt.getTarget());
		String jsp="<a href=\""+elt.getTarget()+"\"><bean:message key=\""+propEntry+"\"/></a>\n";
		data.addToJspCode(jsp);
	}

	@Override
	public void visit(TagElement elt, Data data) {
		System.out.println("NOT Implemented: "+"TagElement");
		
	}

	@Override
	public void visit(Option elt, Data data) {
		String propEntry=WebSiteData.generateLabelID();
		WebSiteData.addRessource(propEntry, elt.getText());
		String jsp="<option value=\""+elt.getValue()+"\"><bean:message key=\""+propEntry+"\"/></option>\n";
		data.addToJspCode(jsp);
	}

	

	@Override
	public void visit(Paragraph elt, Data data) {
		String propEntry=WebSiteData.generateLabelID();
		WebSiteData.addRessource(propEntry, elt.getValue());
		String jsp="<bean:message key=\""+propEntry+"\"/>\n";
		data.addToJspCode(jsp);
	}

	@Override
	public void visit(Radio elt, Data data) {
		data.addFormElement(elt);
		String propEntry=WebSiteData.generateLabelID();
		WebSiteData.addRessource(propEntry, elt.getValue());
		String jsp="<input type=\"radio\" name=\""+elt.getName()+" value=\""+elt.getValue()+"\"><bean:message key=\""+propEntry+"\"/>\n";
		data.addToJspCode(jsp);
	}

	@Override
	public void visit(Reset elt, Data data) {
		//data.addFormElement(elt);
		String jsp="<html:reset>"+elt.getValue()+"</html:reset>\n";
		data.addToJspCode(jsp);
	}

	@Override
	public void visit(Submit elt, Data data) {
		//data.addFormElement(elt);
		String jsp="<html:submit>"+elt.getValue()+"</html:submit>\n";
		data.addToJspCode(jsp);
	}

	@Override
	public void visit(Text elt, Data data) {
		data.addFormElement(elt);
		String type=(elt.getType()==TextType.EMAIL || elt.getType()==TextType.SIMPLE_TEXT)?"text":"password";
		String jsp="<html:"+type+" property=\""+elt.getName()+"\" size=\""+elt.getSize()+"\" maxlength=\""+elt.getMaxlength()+"\"/>\n";		
		data.addToJspCode(jsp);
	}

	@Override
	public void visit(TextArea elt, Data data) {
		data.addFormElement(elt);
		String jsp="<textarea rows= \""+elt.getRows()+"\" cols=\""+elt.getCols()+"\" name=\""+elt.getName()+"\"> "; 
		jsp+=elt.getValue()+"</textarea>\n";		
		data.addToJspCode(jsp);
	}

	
	@Override
	public void visit(Page elt, Data data) {
		
		EList<TagElement> tags=elt.getTags();
		String jsp="";		
		data.setPageTitle(elt.getTitle());
		WebSiteData.addRessource("page.title."+elt.getTitle(), elt.getTitle());
		
		for(TagElement e:tags){
			e.accept(this, data);
		}
		
		
	}
	@Override
	public void visit(WebSite elt, Data data) {
		for(Page page:elt.getPages()){
			Data pageData=new Data();
			
			page.accept(this, pageData);
			
			WebSiteData.addData(pageData);	
		}
	}
	

}
